package cn.wolfcode.crud.web.controller;


import cn.wolfcode.crud.domain.Customer;
import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.domain.SystemDictionaryItem;
import cn.wolfcode.crud.qo.CustomerQuery;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.service.*;
import cn.wolfcode.crud.util.JsonResult;
import cn.wolfcode.crud.domain.Customer;
import cn.wolfcode.crud.service.ICustomerService;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequiresPermissions(value = {"customer:potentialList","客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo){
        //只查潜在客户状态的数据
        qo.setStatus(Customer.STATUS_COMMON);
        //不同的人员进来看到的数据是不一样的,管理和经理都可以看,普通人员看自己的
        //判断当前用户拥有的角色
        Subject subject =SecurityUtils.getSubject();
        //判断当前用户是不是经理或者管理员,是的话没必要
        //如果不是就设置一下传入当前自己的id,去数据库查条件是能看的只有自己的客户
        if (!(subject.hasRole("Admin")||subject.hasRole("Market_Manager"))){
           Employee employee = (Employee)subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询销售人员下拉框数据,只显示经理或销售员的角色
        List<Employee> sollers = employeeService.selectByRoleSn("Market_Manager","Market");
        model.addAttribute("sollers", sollers);

        //查询职业下拉框的数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs", jobs);

        //查询来源下拉框的数据
        List<SystemDictionaryItem> sources= systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources", sources);

        //查询交流方式的下拉框的数据
        List<SystemDictionaryItem> ccts= systemDictionaryItemService.selectByParentSn("communicationMethod");
        model.addAttribute("ccts",ccts);
        return "customer/potentialList";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }


    @RequiresPermissions(value = {"customer:potentialList","客户池页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQuery qo){
        //只查客户池状态的数据
        qo.setStatus(Customer.STATUS_POOL);

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //查询销售人员下拉框数据,只显示经理或销售员的角色
        List<Employee> sollers = employeeService.selectByRoleSn("Market_Manager","Market");
        model.addAttribute("sollers", sollers);

        //查询职业下拉框的数据
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs",jobs);

        //查询来源下拉框的数据
        List<SystemDictionaryItem> sources= systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources", sources);


        return "customer/poolList";
    }


    @RequestMapping("/updateStatus")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户修改状态"},logical = Logical.OR)
    @ResponseBody
    public JsonResult updateStatus(Customer customer){
        customerService.updateStatus(customer);
        return new JsonResult();
    }

}
